const mysql = require('mysql')
const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'root123456',
  database: 'reader'
})

const db = {}
db.query = function(sql, params, callBack){
  pool.getConnection((err, connection) => {
    if(err){
      callBack(err, null)
      return
    }

    connection.query(sql, params, (err, results, fields) => {
      console.log(`${sql}=>${params}`)
      connection.release()
      if(results != null) {
        const dataString = JSON.stringify(results);
        callBack(err, JSON.parse(dataString))
      } 
      else{
        callBack(err, null)
      }
      
    })
  })
}

module.exports = db